package cn.com.wanywhn.servlet;

import cn.com.wanywhn.bean.Employee;
import cn.com.wanywhn.dao.EmployeeDAO;
import cn.com.wanywhn.factory.EmployeeDAOFactory;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Objects;

@WebServlet(name = "ServletUserRegister")
public class ServletUserRegister extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext=getServletContext();
        RequestDispatcher dispatcher=null;
        request.setCharacterEncoding("UTF-8");//设置参数编码格式
        String employeeID=request.getParameter("employeeID");
        String password=request.getParameter("password");
        String eName=request.getParameter("employeeName");
        boolean eGender= Objects.equals(request.getParameter("eGender"), "男");
        DateFormat dateFormat=new SimpleDateFormat();
        java.util.Date date=new java.util.Date();
        try {
            date=dateFormat.parse(request.getParameter("eBirthDay"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date eBirth=new Date(date.getTime());
        String eOfficePhone=request.getParameter("eOfficePhone");
        String eAddress=request.getParameter("eAddress");
        Date eJoinTime=new Date(new java.util.Date().getTime());
        boolean isLead=false;
        if(employeeID.isEmpty()||password.isEmpty()){
            request.setAttribute("error","请补全信息");
            dispatcher=servletContext
                    .getRequestDispatcher("/statusRecognise.jsp");
        }else{
            EmployeeDAO employeeDAO=
                    EmployeeDAOFactory.getEmployeeDAOInstance();
            Employee employee=new Employee();
            //TODO 信息细节处理
            employee.setEmployeeID(Integer.parseInt(employeeID));
            employee.setPassword(password);
            employee.setEmployeeName(eName);
            employee.setEmployeeSex(eGender);
            employee.setEmployeeBirth(eBirth);
            employee.setEmployeePhone(eOfficePhone);
            employee.setEmployeePlace(eAddress);
            employee.setJoinTime(eJoinTime);
            employee.setLead(isLead);
            employeeDAO.addEmployee(employee);
            //TODO 出错处理
            request.getSession().setAttribute("employee",employee);
            response.sendRedirect("index.jsp");
            return ;
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);

    }
}
